package com.zxd.controller.backend;

import com.zxd.controller.common.CommonController;
import com.zxd.entity.Master;
import com.zxd.entity.vo.BaseVo;
import com.zxd.service.GroupService;
import com.zxd.service.MasterService;
import com.zxd.service.RoleModuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 管理员管理
 * Created by 涂奕恒 on 2019/01/28.
 */
@Controller
@RequestMapping(value = "backend/master")
public class MasterController extends CommonController {

    @Autowired
    private MasterService masterService;

    @Autowired
    private RoleModuleService roleModuleService;

    @Autowired
    private GroupService groupService;

    @RequestMapping("/list")
    public String index() {
        return "backend/master/list";
    }

    @RequestMapping("/getData")
    @ResponseBody
    public BaseVo getData(String keyWord, Integer page, Integer limit) {
        return masterService.getData(keyWord, page, limit);
    }

    @RequestMapping("/show")
    public String show() {
        return "backend/master/info";
    }

    @RequestMapping("/save")
    @ResponseBody
    public Integer save(Master master, Integer roleId) {
        return masterService.saveInfo(master, roleId);
    }

    /**
     * 升级管理员信息
     */
    @RequestMapping("/upStatus")
    @ResponseBody
    public Integer upStatus(Integer id) {
        try {
            return masterService.upStatus(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 删除管理员信息
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Integer delete(Integer id) {
        try {
            masterService.deleteById(id);
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 批量删除管理员信息
     */
    @RequestMapping("/batchDel")
    @ResponseBody
    public Integer batchDel(String ids) {
        try {
            return masterService.batchDel(ids);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @RequestMapping("/powerInfo")
    public String powerInfo() {
        return "backend/master/powerInfo";
    }

    @RequestMapping("/powerSearch")
    @ResponseBody
    public List<Integer> powerSearch(Integer roleId) {
        return roleModuleService.getRoleModule(roleId);
    }

    @RequestMapping("/savePower")
    @ResponseBody
    public Integer savePower(Integer roleId, String name, String ids) {
        return roleModuleService.savePower(roleId, name, ids);
    }

    @RequestMapping("/saveGroup")
    @ResponseBody
    public Integer saveGroup(Integer id, String name, Integer parentId) {
        return groupService.saveGroup(id, name, parentId);
    }

    @RequestMapping("/getGroup")
    @ResponseBody
    public BaseVo getGroup(Integer id) {
        return groupService.getGroup(id);
    }

    @RequestMapping("/deleteGroup")
    @ResponseBody
    public Integer deleteGroup(Integer id) {
        try {
            groupService.deleteById(id);
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
}